package com.chen.androidstudy.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity
import com.base.library.base.BaseVM
import com.base.library.base.BaseVMActivity
import com.chen.androidstudy.R
import com.chen.androidstudy.databinding.ActivityLocatonBinding
import com.chen.androidstudy.utils.GPSManage

/**
 *
 * @Author:        CHong
 * @CreateDate:    2024/7/23 0023
 * @Description: 原生定位功能
 */
class LocationActivity : BaseVMActivity<LocationViewModel, ActivityLocatonBinding>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_locaton
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        ) {
            initGps()
        } else {
            requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION), 1)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1) {
            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，发送短信
                Toast.makeText(this@LocationActivity, "有定位的权限", Toast.LENGTH_SHORT).show()
                initGps()
            } else {
                // 权限被拒绝，可以提示用户或者采取其他行动
                Toast.makeText(this@LocationActivity, "无定位权限", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun initGps() {
        GPSManage.getInstance().initGps(this@LocationActivity)
        GPSManage.getInstance().addLocationBackListener {
            val addressStr = GPSManage.getInstance().addressStr
            var msg = GPSManage.getInstance().locationMsg + addressStr
            mVB.tvContent.text = msg
        }
        val addressStr = GPSManage.getInstance().addressStr
        mVB.tvContent.text = addressStr

        val enabled = GPSManage.getInstance().isLocationEnabled()
        Log.e("chen", "------- " + enabled)
        Toast.makeText(this, "enabled:" + enabled, Toast.LENGTH_SHORT).show()
        if (!enabled){
            goToLocationSettings()
        }
    }

    override fun onDestroy() {
        GPSManage.getInstance().unregister()
        super.onDestroy()
    }

// 在您的Activity中调用此方法
    public fun goToLocationSettings() {
        var intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        if (intent.resolveActivity(getPackageManager()) != null) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent);
        }
    }
}